﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SoufunData
{
    public partial class LandStatisticsForm : DevExpress.XtraEditors.XtraForm
    {
        public LandStatisticsForm()
        {
            InitializeComponent();
        }

        private void LandInfoListForm_Load(object sender, EventArgs e)
        {
            dateEdit_CtimeStart.DateTime = DateTime.Now.AddYears(-3);
            dateEdit_CtimeEnd.DateTime = DateTime.Now;



            gridControl2.DataSource = null;
            gridView2.GroupPanelText = "正在统计中...";

            gridControl1.Size = gridControl2.Size;
            gridControl1.Location = gridControl2.Location;

            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
          
        private readonly SqlClass _sqlClass = new SqlClass();
 

        private void LoatData()
        {
            StringBuilder strWhere = new StringBuilder();
            switch (radioGroup1.SelectedIndex)
            {
                    case 0:
                    strWhere.Append("CTime between '");
                    break;
                case 1:
                    strWhere.Append("TransactionTime between '");
                    break;
            }
            
            strWhere.Append(dateEdit_CtimeStart.DateTime);
            strWhere.Append("' and '");
            strWhere.Append(dateEdit_CtimeEnd.DateTime);
            strWhere.Append("' ");

            if (imgComBox_Board.SelectedIndex!=0)
            {
                strWhere.Append(" and Board='");
                strWhere.Append(imgComBox_Board.Properties.Items[
                    imgComBox_Board.SelectedIndex].Description);
                strWhere.Append("' ");
            }

            if (imgCombo_LandStatus.SelectedIndex != 0)
            {
                strWhere.Append(" and LandStatus='");
                strWhere.Append(imgCombo_LandStatus.Properties.Items[
                    imgCombo_LandStatus.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgCombo_xzq.SelectedIndex != 0)
            {
                strWhere.Append(" and AdministratiVeregion='");
                strWhere.Append(imgCombo_xzq.Properties.Items[
                    imgCombo_xzq.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgCombox_PlanedUse.SelectedIndex != 0)
            {
                strWhere.Append(" and PlanedUse='");
                strWhere.Append(imgCombox_PlanedUse.Properties.Items[
                    imgCombox_PlanedUse.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (textEdit_Group.Text.Trim()!="")
            {
                strWhere.Append(" and `Group` like '%");
                strWhere.Append(textEdit_Group.Text.Trim());
                strWhere.Append("%' ");
            }
            try
            {
                DataSet ds =null;
                switch (radioGroup1.SelectedIndex)
                {
                    case 0:
                        ds = _sqlClass.GetLandStatisticsResultByLaunch(strWhere.ToString());
                           if (ds == null || ds.Tables.Count == 0)
                                {
                                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                                    return;
                                }
          
                                BeginInvoke(new MethodInvoker(() =>
                                {
                                    gridControl2.Visible = true;
                                    gridControl1.Visible = false;
                                    gridControl2.DataSource = ds.Tables[0];
                                    gridView2.GroupPanelText = "共记"
                                        + ds.Tables[0].Rows.Count + "条数据";
                                    btn_select.Enabled = true;
                                }
                                ));
                        break;
                    case 1:
                        ds = _sqlClass.GetLandStatisticsResultByDeal(strWhere.ToString());
                         if (ds == null || ds.Tables.Count == 0)
                                {
                                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                                    return;
                                }
          
                                BeginInvoke(new MethodInvoker(() =>
                                    {
                                        gridControl2.Visible = false;
                                        gridControl1.Visible = true;
                                    gridControl1.DataSource = ds.Tables[0];
                                    gridView1.GroupPanelText = "共记"
                                        + ds.Tables[0].Rows.Count + "条数据";
                                    btn_select.Enabled = true;
                                }
                                ));
                        break;
                         
                }
             
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

        
        private void btn_select_Click(object sender, EventArgs e)
        {
            if (!CheckDateEdit())
            {
                return;
            }
            gridControl2.DataSource = null;
            gridView2.GroupPanelText = "正在查询...";
            gridControl1.DataSource = null;
            gridView1.GroupPanelText = "正在查询...";
            btn_select.Enabled = false;
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
 
        private bool CheckDateEdit()
        {

            if (dateEdit_CtimeStart.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_CtimeEnd.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_CtimeStart.DateTime > dateEdit_CtimeEnd.DateTime)
            {
                XtraMessageBox.Show("您选择的【公告时间】范围中的起始日期不能大于截止日期！");
                dateEdit_CtimeStart.Focus();
                return false;
            }
           
            return true;
        }



        private void btn_toExcel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "土地数据统计";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (gridControl2.Visible ==true)
                {
                    gridView2.ExportToXls(saveFileDialog1.FileName);    
                }
                else
                {
                    gridView1.ExportToXls(saveFileDialog1.FileName);    
                }
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

 

    }
}